package com.tour.controller;

import com.tour.bean.BaseParam;
import com.tour.bean.Consumer;
import com.tour.bean.ReturnT;
import com.tour.service.ConsumerService;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * consumer
 * @author 张梦兴
 * @date 2020/03/13
 */
@Controller
@RequestMapping(value = "/Consumer")
public class ConsumerController {

    private final Logger log=Logger.getLogger(ConsumerController.class);

    @Resource
    private ConsumerService consumerService;

    /**
     * [新增]相当于注册
     * @author 张梦兴
     * @date 2020/03/13
     **/
    @RequestMapping("/insert")
    public Object insert(Consumer consumer){
        //默认头像
        consumer.setConsPhoto("/assets/img/tox.jpg");
        log.debug(consumer);
        consumerService.insert(consumer);
        return "redirect:/userLogin" ;
    }

    /**
     * [刪除]
     * @author 张梦兴
     * @date 2020/03/13
     **/
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable("id") int id){

        return consumerService.delete(id);
    }

    /**
     * [更新]
     * @author 张梦兴
     * @date 2020/03/13
     **/
    @RequestMapping("/update")
    public Object update(Consumer consumer){
        return consumerService.update(consumer);
    }

    /**
     * [查询] 根据主键 id 查询
     * @author 张梦兴
     * @date 2020/03/13
     **/
    @RequestMapping("/load")
    public Object load(int id){
        return consumerService.load(id);
    }

    /**
     * [查询] 分页查询
     * @author 张梦兴
     * @date 2020/03/13
     **/
    @RequestMapping("/pageList")
    @ResponseBody
    public Map<String, Object> pageList(BaseParam baseParam){
        JSONObject json = baseParam.getParamJson();
        int offset=json.getInt("offset");
        int pagesize=json.getInt("pagesize");
        return consumerService.pageList(offset, pagesize);
    }

    /**
     * 验证邮箱是否存在
     * 注意当server必需返回形如：{“valid”,true or false} 的json数据格式
     * @param emailVal
     * @return
     */
    @RequestMapping("/validEmail")
    @ResponseBody
    public Object validEmail(@RequestParam(value = "emailVal", required = true)String emailVal){
        log.debug("即将验证的email："+emailVal);
        Map<String,Boolean> validResult=new HashMap<>();
        ReturnT<String> result = consumerService.loadByEmail(emailVal);
        int code = result.getCode();
        if (200==code) {
            validResult.put("valid", true);
            return validResult;
        }
        validResult.put("valid", false);
        return  validResult;
    }

    /**
     * 用户登录
     * @param userEmail
     * @param userPassword
     * @param request
     * @param map
     * @return
     */
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String userEmail, String userPassword, HttpServletRequest request, Map<String,Object> map){
        log.debug("登录的邮箱："+userEmail+",密码："+userPassword);
        if(StringUtils.isEmpty(userEmail) || StringUtils.isEmpty(userPassword)){
            return ReturnT.FAIL;
        }
        //request.getSession().setAttribute("userInfo", "123");
        Consumer consumer = consumerService.login(userEmail, userPassword);
        if(null==consumer){
            return ReturnT.FAIL;
        }
        request.getSession().setAttribute("userInfo", consumer);
        return ReturnT.SUCCESS;
    }

    /**
     * 用户退出
     * @return
     */
    @RequestMapping("/userLogout")
    @ResponseBody
    public Object userLogout(HttpServletRequest request){
        request.getSession().removeAttribute("userInfo");
        return ReturnT.SUCCESS;
    }

}

